﻿using Api_Core_Spider.Models;
using Api_Core_Spider.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Api_Core_Spider.Controllers
{
    /// <summary>
    /// 激活控制器，提供软件激活相关的API接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ActivationController : ControllerBase
    {
        private readonly IActivationService _activationService;
        private readonly IMachineCodeGenerator _machineCodeGenerator;
        private readonly ILogger<ActivationController> _logger;

        public ActivationController(IActivationService activationService, IMachineCodeGenerator machineCodeGenerator, ILogger<ActivationController> logger)
        {
            _activationService = activationService;
            _machineCodeGenerator = machineCodeGenerator;
            _logger = logger;
        }

        /// <summary>
        /// 【使用方】获取当前机器码
        /// </summary>
        /// <returns>机器信息</returns>
        [HttpGet("machinecode")]
        public IActionResult GetMachineCode()
        {
            try
            {
                var machineInfo = _machineCodeGenerator.GetMachineInfo();
                return Ok(machineInfo);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = $"获取机器码失败: {ex.Message}" });
            }
        }



        /// <summary>
        /// 【授权方】生成产品密钥（管理接口）
        /// </summary>
        /// <param name="productId">产品ID</param>
        /// <param name="userId">用户ID</param>
        /// <param name="days">有效期天数</param>
        /// <returns>产品密钥</returns>
        [HttpPost("generatekey")]
        [ApiExplorerSettings(IgnoreApi = true)] // 隐藏此接口，仅用于管理
        public IActionResult GenerateProductKey(string productId, string userId, int days)
        {
            try
            {
                // 实际应用中应添加权限验证
                string productKey = _activationService.GenerateProductKey(productId, userId, days);
                return Ok(new { productKey });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = $"生成产品密钥失败: {ex.Message}" });
            }
        }


        /// <summary>
        /// 激活软件
        /// </summary>
        /// <param name="request">激活请求</param>
        /// <returns>激活响应</returns>
        [HttpPost("activate")]
        public IActionResult Activate([FromBody] ActivationRequest request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                var response = _activationService.Activate(request);

                if (response.Success)
                {
                    return Ok(response);
                }
                else
                {
                    return BadRequest(new { error = response.ErrorMessage });
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = $"激活过程中发生错误: {ex.Message}" });
            }
        }
        /// <summary>
        /// 验证激活码
        /// </summary>
        /// <param name="request">验证请求</param>
        /// <returns>验证响应</returns>
        [HttpPost("validate")]
        public IActionResult Validate([FromBody] ValidationRequest request)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                var response = _activationService.Validate(request);
                return Ok(response);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = $"验证过程中发生错误: {ex.Message}" });
            }
        }


    }
}
